package com.shopReduce.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shop.bean.Shop;
import com.shop.feign.ShopFeign;
import com.shopReduce.bean.ReduceProgramme;
import com.shopReduce.bean.ShopReduce;
import com.shopReduce.dao.ShopReduceMapper;
import com.shopReduce.service.IReduceProgrammeService;
import com.shopReduce.service.IShopReduceService;

import shopBean.common.Enum.StatusAll;
import shopBean.common.bean.RestResult;
import shopBean.common.responseCode.Status;
import shopBean.common.utils.DateCommonUtils;
import shopBean.common.utils.RestResultGenerator;
import shopBean.common.utils.UUIDGenerator;
@Service("shopReduceService")
public class ShopReduceServiceImpl implements IShopReduceService {
	@Autowired
	ShopReduceMapper shopReduceMapper;
	@Autowired
	ShopFeign shopFeign;
	@Autowired
	IReduceProgrammeService reduceProgrmmeService;
	
	@Override
	@Transactional
	public RestResult<ShopReduce> insert(ShopReduce shopReduce) throws ParseException {
		RestResult<Shop> shopResult = shopFeign.getShopById(shopReduce.getShopId());
		if(shopResult == null ||!StringUtils.equals(shopResult.getStatusCode(), Status.sys_success)||shopResult.getData()  == null || shopResult.getData().getStatus()==StatusAll.delete ) {
			return RestResultGenerator.genErrorResult("商品不存在", null, Status.shop_is_exist);
		}
		ReduceProgramme reduceProgramme = reduceProgrmmeService.get(shopReduce.getReduceId());
		if(reduceProgramme == null||reduceProgramme.getStatus()==StatusAll.delete) {
			return RestResultGenerator.genErrorResult("优惠方案不存在", null, Status.shop_is_exist);
		}
		Date shopReduceExpireDate = DateUtils.parseDate(shopReduce.getExpireDate(), "yyyy-MM-dd HH:mm:ss");
		Date reduceProgrammeExpireDate = DateUtils.parseDate(reduceProgramme.getExpireDate(),  "yyyy-MM-dd HH:mm:ss.SSS");
		if(shopReduceExpireDate.getTime()>reduceProgrammeExpireDate.getTime()) {
			shopReduce.setExpireDate(reduceProgramme.getExpireDate());
		}
		shopReduce.setCreateDate(DateCommonUtils.getCurrent());
		shopReduce.setId(UUIDGenerator.generate());
		this.shopReduceMapper.insert(shopReduce);
		return RestResultGenerator.genSuccessResult(shopReduce);
	}
	@Transactional
	public RestResult deleteById(String id) {
		// TODO Auto-generated method stub
		return RestResultGenerator.genSuccessResult(shopReduceMapper.deleteById(id));
	}

	@Override
	@Transactional
	public RestResult updateById(ShopReduce shopReduce) {
		// TODO Auto-generated method stub
		return RestResultGenerator.genSuccessResult(shopReduceMapper.updateById(shopReduce));
	}

	@Override
	public long listCount(ShopReduce search) {
		// TODO Auto-generated method stub
		return this.shopReduceMapper.listCount(search);
	}

	@Override
	public List<ShopReduce> list(ShopReduce search) {
		// TODO Auto-generated method stub
		return this.shopReduceMapper.list(search);
	}

	@Override
	public ShopReduce get(String id) {
		// TODO Auto-generated method stub
		return shopReduceMapper.get(id);
	}

	public static void main(String[] args) throws ParseException {
		System.out.println( DateUtils.parseDate("2018-07-22 11:11:11.0",  "yyyy-MM-dd HH:mm:ss.SSS"));
	}
	
}
